package tv.accedo.memory.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tv.accedo.memory.dao.AchiDao;
import tv.accedo.memory.dao.UserDao;
import tv.accedo.memory.model.Achi;
import tv.accedo.memory.model.User;

public class CardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter(); 
		System.out.println("Server get one request from client side!"); 
		 
		UserDao userDao = new UserDao();
		AchiDao achiDao = new AchiDao(); 
		User user = new User();
		Achi achi = new Achi(); 
		String username = request.getParameter("username");
		user = userDao.getUserByUsername(username); 
		
		System.out.println("ggggggggggggggggggg" + userDao.getCardNumByUserIdandAchiId(1, 2));
		
		
		
		int card_num; 
		String card_sn; 
		String jsonString = "]}";
		for (Iterator<Achi> iter = user.getAchis().iterator(); iter.hasNext(); ) {
			achi = iter.next(); 
			card_sn = achiDao.getCardsnByAchisn(achi.getAchi_sn()); 
			card_num = userDao.getCardNumByUserIdandAchiId(user.getId(), achi.getId()); 
			System.out.println("kkkkkkkkkkkkkkkkkkkkkkk" + userDao.getCardNumByUserIdandAchiId(1, 2));
			System.out.println(user.getId() + " " + achi.getCard_sn() + " " + card_num); 
			jsonString = "{card_sn:'" + card_sn + "',card_num:" + card_num + "}," + jsonString; 
		}
		
		jsonString = "{cards:[" + jsonString; 
		System.out.println(jsonString); 
		out.print(jsonString); 
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}